#!/bin/bash
# Description: This shell script used to generate patch file
#      Author: huqiu <huqiu12@126.com>
#     Version: 1.0.0

# this project absolute path
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)

# shell script will exit once get command error
#set -e

#+-------------------------+
#| Shell script functions  |
#+-------------------------+

function pr_error() {
    echo -e "\033[40;31m $1 \033[0m"
}

function pr_warn() {
    echo -e "\033[40;33m $1 \033[0m"
}

function pr_info() {
    echo -e "\033[40;32m $1 \033[0m"
}

# parser configure file and export environment variable
function do_export()
{
    export SDKVER=$(basename $1)
}

function gen_patch()
{
	SRC_NAME=$1
    SRC_PATH=$PRJ_PATH/$SDKVER/$1

    if [ ! -d $SRC_PATH ] ;  then
        pr_error "\nERROR: source code $SRC_PATH not exist, exit now\n\n"
        exit;
    fi

    pr_info "generate patch file for $SRC_NAME"
    cd $SRC_PATH

    git add * 2> /dev/null

    git diff --cached > $PRJ_PATH/$SRC_NAME.patch
}

function do_patch()
{
	gen_patch kernel
	gen_patch buildroot
}

if [ $# != 1 ] ; then
    pr_info "$0 usage example:"
    pr_info "$0 sdkv1.4.0_linux5.10"
    exit
fi

do_export $1
do_patch
